package com.heima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.common.PageResult;
import com.heima.mapper.SkuClassMapper;
import com.heima.mapper.SkuMapper;
import com.heima.pojo.Sku;
import com.heima.pojo.SkuClass;
import com.heima.pojo.dto.SkuClassPageQueryDTO;
import com.heima.pojo.vo.SkuVo;
import com.heima.service.SkuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class SkuServiceImpl implements SkuService {

    @Autowired
    private SkuMapper skuMapper;
    @Autowired
    private SkuClassMapper skuClassMapper;

    @Override
    public Boolean add(Sku sku) {
        int result = skuMapper.insert(sku);
        sku.setCreateTime(LocalDateTime.now());
        sku.setUpdateTime(LocalDateTime.now());
        return result != 0;
    }

    @Override

    public PageResult skuQuery(SkuClassPageQueryDTO dto) {
        dto.check();

        PageHelper.startPage(dto.getPageIndex(), dto.getPageSize());
        Page<SkuVo> page = skuMapper.skuQuery(dto);
        return new PageResult(dto.getPageIndex(), dto.getPageSize(), page.getPages(), page.getTotal(), page.getResult());
    }

    @Override
    public Boolean update(String skuId, Sku sku) {
        sku.setUpdateTime(LocalDateTime.now());
        sku.setSkuId(Long.parseLong(skuId));
        int result = skuMapper.updateById(sku);
        return result != 0;
    }

    @Override
    public Sku findById(String skuId) {
        return skuMapper.selectById(skuId);
    }

    @Override
    public List<SkuVo> businessTop10(Integer businessId) {
        return skuMapper.businessTop10(businessId);
    }


}
